<?php

namespace Admin\Controller;

class ActivityController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Activity");
    }

    //首页
    public function index() {

        $requestMap = $this->convertRequestToMap();

        //分页
        $count = $this->currentModel->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取列表
        $list = $this->currentModel->queryForList($page, $requestMap);

        $this->assign("activityStatus", C("ACTIVITY_STATUS"));
        $this->assign("activityType", C("ACTIVITY_TYPE"));
        $this->assign("activityPay", C("ACTIVITY_PAY"));
        $this->assign('list', $list);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }

    public function add() {
        $this->assign("activityTypeEnum", C("ACTIVITY_TYPE"));
        $this->assign("activityPayEnum", C("ACTIVITY_PAY"));
        $this->display();
    }

    public function viewJoin() {

        $activityId = $_REQUEST['id'];

        $map['aj.activity_id'] = $activityId;

        $list = D("ActivityJoin")->alias('aj')
            ->join("left join __CUSTOMER__ c on aj.customer_id = c.customer_id")
            ->field("aj.*, c.nickname")
            ->where($map)
            ->order("aj.join_id desc")
            ->select();

        $this->assign('statusEnum', C('ACTIVITY_JOIN_STATUS'));
        $this->assign("list", $list);
        $this->display();

    }
    
    public function addPost() {

        $data['title'] = I("post.title");
        $data['begin_time'] = I('post.beginTime');
        if(I('post.activityPay') == 1)
            $data['price'] = I('post.price');
        else
            $data['price'] = 0;
        $data['location'] = I('post.location');
        $data['description'] = html_entity_decode(I('post.description'));
        $data['upper_limit'] = I('post.upperLimit');
        $data['title_image'] = I('post.titleImage');
        $data['create_time'] = getCurrentDate();
        $data['activity_type'] = I('post.activityType');
        $data['activity_pay'] = I('post.activityPay');
        $data['category_id'] = I('post.categoryId');
        $data["end_time"] = I("post.endTime");
        $data["contact"] = I("post.contact");
        $data["activity_link"] = I("post.activityLink");
        if(I('post.activityType') == C("ACTIVITY_TYPE.NEWS"))
            $data["status"] = C("ACTIVITY_STATUS.NEWS");
        $this->currentModel->add($data);

        $this->success("添加成功！", U('activity/index'));
    }

    public function edit() {

        $id = I('get.id');

        $activity = $this->currentModel->queryById($id);

        $this->assign("activityTypeEnum", C("ACTIVITY_TYPE"));
        $this->assign("activityPayEnum", C("ACTIVITY_PAY"));
        $this->assign('info', $activity);
        $this->display();
    }
    
    public function editPost() {

        $id = I('post.id');

        $data = $this->currentModel->queryById($id);

        $data['title'] = I("post.title");
        $data['begin_time'] = I('post.beginTime');
        if(I('post.activityPay') == 1)
            $data['price'] = I('post.price');
        else
            $data['price'] = 0;
        $data['location'] = I('post.location');
        $data['description'] = html_entity_decode(I('post.description'));
        $data['upper_limit'] = I('post.upperLimit');
        $data['title_image'] = I('post.titleImage');
        $data['activity_type'] = I('post.activityType');
        $data['activity_pay'] = I('post.activityPay');
        $data['category_id'] = I('post.categoryId');
        $data["end_time"] = I("post.endTime");
        $data["contact"] = I("post.contact");
        $data["activity_link"] = I("post.activityLink");
        if(I('post.activityType') == C("ACTIVITY_TYPE.NEWS"))
            $data["status"] = C("ACTIVITY_STATUS.NEWS");

        $this->currentModel->updateInfo($data);

        $this->success("更新成功！", U('activity/index'));

    }


}
